package Bi_Tree.level_traversal;

import Bi_Tree.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/*给定一棵二叉树的根节点 root ，请找出该二叉树中每一层的最大值。*/
public class lc515 {
    public List<Integer> largestValues(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        List<Integer> result=new ArrayList<>();
        TreeNode node=root;
        queue.offer(node);
        while(!queue.isEmpty()){
            List<Integer> list=new ArrayList<>();
            int len= queue.size();
            while (len>0){
                node= queue.poll();
                list.add(node.val);
                if(node.left!=null) queue.offer(node.left);
                if(node.right!=null) queue.offer(node.right);
                len--;
            }
            int max= list.get(0);
            for (int i = 1; i < list.size() ; i++) {
                if (max<list.get(i)) max=list.get(i);
            }
            result.add(max);
        }
        return result;

    }
}
